package com.cetcs.kmga.common;

import com.ccsc.auth.entity.ShiroSysUser;
import com.ccsc.auth.entity.ShiroUser;
import com.ccsc.auth.utils.SSOResourceUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by houbo on 2016/3/2.
 */
public class BaseController {
    @Autowired
    private MessageSource messageSource;
    public ExecutorService fixedThreadPool  = Executors.newFixedThreadPool(5);

    public Data getParam(WebRequest request) {
        Iterator<String> it = request.getParameterNames();
        Data result = new Data();
        while (it.hasNext()) {
            String key = it.next();
            result.put(key, request.getParameter(key));
        }
        return result;
    }
    public BaseResponse ok() {
        return new BaseResponse(HttpStatus.SUCCESS, "");
    }
    public BaseResponse ok(Object data) {
        return new BaseResponse(HttpStatus.SUCCESS, data);
    }

    public BaseResponse ok(int code, String msg) {
        return new BaseResponse(HttpStatus.SUCCESS, msg, true);
    }

    public BaseResponse error(int code, Object data) {
        return new BaseResponse(code, data);
    }

    public BaseResponse error(int code, String msg) {
        return new BaseResponse(code, msg, false);
    }

    public BaseResponse error(String code, Object data) {
        return new BaseResponse(code, data);
    }

    public BaseResponse error(String code, String msg) {
        return new BaseResponse(code, msg, false);
    }

    public BaseResponse error(String code) {
        return error(Integer.parseInt(code));
    }

    public BaseResponse error(int code) {
        BaseResponse baseResponse = null;
        switch(code){
            case HttpStatus.NOTFOUND: baseResponse = new BaseResponse(code, getMessageSource(String.valueOf(HttpStatus.NOTFOUND)));break;
            case HttpStatus.EXCEPTION: baseResponse = new BaseResponse(code, getMessageSource(String.valueOf(HttpStatus.EXCEPTION)));break;
            case HttpStatus.FORBIDDEN: baseResponse = new BaseResponse(code, getMessageSource(String.valueOf(HttpStatus.FORBIDDEN)));break;
            default: baseResponse = new BaseResponse(code, getMessageSource(String.valueOf(code)),false);break;
        }
        return baseResponse;
    }

    public String getMessageSource(String code) {
        return messageSource.getMessage(code, new Object[] {}, Locale.SIMPLIFIED_CHINESE);
    }

    public Session getSession() {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        return session;
    }

    public ShiroUser getSessionUser() {
        Session session = getSession();
        ShiroUser shiroUser = new ShiroUser();
        ShiroSysUser shiroSysUser = session != null ? (ShiroSysUser) session.getAttribute("loginSysUser") : null;
        if(shiroSysUser!=null && shiroSysUser.getUserId()!=null){
            shiroUser = SSOResourceUtil.getUserById(String.valueOf(shiroSysUser.getUserId()));
            session.setAttribute("loginUser", shiroUser);
        }
        return shiroUser;
    }

//    public ShiroUser getSessionUser() {
//
//
////        Session session = getSession();
////        return session != null ? (User) session.getAttribute("loginUser") : null;
//        //index直接登录
//        ShiroUser sessionUser = new ShiroUser();
//        sessionUser.setAccount("superAdmin");
//        sessionUser.setUserId(1);
//        sessionUser.setPassword("123456");
//        //sessionUser.setPassword("db79be8558f494216b58814577865cde");
//        sessionUser.setUserName("Administrator");
//        sessionUser.setUserDesc("Administrator");
//        sessionUser.setTelephone("028-88888888");
//        sessionUser.setLastLoginTime(new Date());
//        sessionUser.setStatus(9);
//        sessionUser.setCreatedBy(1);
//        return sessionUser;
//    }



    @ExceptionHandler
    public BaseResponse expHandler(HttpServletRequest request, Exception ex) {
        ex.printStackTrace();
        return this.error(500, getMessageSource("500")+":"+ex.getMessage());
    }

    protected Boolean isArrayNull(List list){
        return list == null || list.size() == 0 ? true : false;
    }
    protected Boolean isStringNull(String string){
        return string == null || "".equals(string) ? true : false;
    }
    /**
     * 字符串选项查找
     * @param arrays
     * @param item
     * @return
     */
    protected Boolean isContainItem(String arrays, String item){
        return arrays.toUpperCase().contains("["+item.toUpperCase()+",") || arrays.toUpperCase().contains(", "+item.toUpperCase()+"]") || arrays.toUpperCase().contains(", "+item.toUpperCase()+",") ? true : false;
    }
}
